package com.ydy.controller;

import com.ydy.pojo.Users;
import com.ydy.pojo.bo.UserBO;
import com.ydy.service.UserService;
import com.ydy.utils.CookieUtils;
import com.ydy.utils.JsonUtils;
import com.ydy.utils.ReturnResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 登录注册相关类
 * @author: dy.yin 2022/1/12 15:39
 */
@Api(value = "注册登录",tags = "用于注册登录的相关接口")
@RestController
@RequestMapping("/loginRegister")
public class LoginRegisterController {

    @Autowired
    private UserService userService;


    /**
     * 检验该注册用户名是否已存在
     * @Title: usernameIsExist
     * @author: dy.yin 2022/1/13 9:31
     * @param: [userName]
     * @return: {@link ReturnResult }
     */
    @ApiOperation(value = "用户名是否存在", notes = "用户名是否存在", httpMethod = "GET")
    @GetMapping("/usernameIsExist")
    public ReturnResult usernameIsExist(@RequestParam String userName){

        if(StringUtils.isBlank(userName)){
            return ReturnResult.errorMsg("用户名不允许为空");
        }

        boolean isExists = userService.queryUsernameIsExist(userName);
        if(isExists){
            return ReturnResult.errorMsg(userName + "该用户名已存在！");
        }

        return ReturnResult.ok();
    }


    /**
     * 用户注册
     * @Title: userRegister
     * @author: dy.yin 2022/1/12 15:49
     * @param: [users]
     * @return: {@link ReturnResult 统一返回}
     */
    @PostMapping("/register")
    @ApiOperation(value = "用户注册",notes = "用户注册接口",httpMethod = "POST")
    public ReturnResult userRegister(@RequestBody @Valid UserBO userBO,
                                     HttpServletRequest request, HttpServletResponse response){

        //1.验证用户名密码不能为空，该步骤做成配置检验，异常在myException里面捕捉
        //2.验证用户名是否存在
        String userName = userBO.getUsername();
        String password = userBO.getPassword();
        String confirmPassword = userBO.getConfirmPassword();

        boolean isExists = userService.queryUsernameIsExist(userName);
        if(isExists){
            return ReturnResult.errorMsg(userName + "该用户名已存在！");
        }

        //3.校验两次密码是否一致
        if(!password.equals(confirmPassword)){
            return ReturnResult.errorMsg("2次密码不一致！");
        }

        //4.注册
        Users userResult = userService.registerUser(userBO);

        CookieUtils.setCookie(request, response, "user",
                JsonUtils.objectToJson(userResult), true);

        return ReturnResult.ok();
    }


    /**
     * 用户登录
     * @Title: Login
     * @author: dy.yin 2022/1/12 16:55
     * @param: [user] 用户信息
     * @return: {@link ReturnResult 统一返回}
     */
    @ApiOperation(value = "用户登录", notes = "用户登录", httpMethod = "POST")
    @PostMapping("/login")
    public ReturnResult login(@RequestBody @Validated UserBO userBO,
            HttpServletRequest request, HttpServletResponse response){
        //1.验证用户名密码不能为空，该步骤做成配置检验，异常在myException里面捕捉
        //2.通过用户名和密码查询用户
        Users userResult = userService.queryUserForLogin(userBO);
        if(Objects.isNull(userResult)){
            return ReturnResult.errorMsg("用户名或者密码错误！");
        }
        //把密码置为空返回
        userResult.setPassword(null);
        CookieUtils.setCookie(request, response, "user",
                JsonUtils.objectToJson(userResult), true);

        return ReturnResult.ok(userResult);
    }

    /**
     * 用户登出
     * @Title: loginOut
     * @author: dy.yin 2022/1/12 17:05
     * @param: [userId] 用户id
     * @return: {@link ReturnResult 统一返回}
     */
    @ApiOperation(value = "用户退出登录", notes = "用户退出登录", httpMethod = "POST")
    @PostMapping("/logout")
    public ReturnResult loginOut(@RequestParam String userId,
                                 HttpServletRequest request,
                                 HttpServletResponse response){
        CookieUtils.deleteCookie(request,response,"user");
        return ReturnResult.ok();
    }
}
